<template>
  <ele-page>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="roleId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>
        <template #display="{ row }">
          <el-tag
            :type="showType(row.display)"
            class="pointer"
            @click="handleDisplay(row)"
          >
            {{ showText(row.display) }}
          </el-tag>
        </template>
        <template #action="{ row }">
          <el-link type="primary" :underline="false" @click="openEdit(row)">
            修改
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="info" :underline="false" @click="openImage(row)">
            二维码
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="danger" :underline="false" @click="remove(row)">
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
    <!-- 显示图片 -->
    <ShowImage v-model="showImage" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import Edit from './components/edit.vue';
  import ShowImage from './components/image.vue';

  import { pageGoods, removeGoods, updateGoodsDisplay } from '@/api/mall';

  const showText = (value) => {
    const cs = {
      1: '显示',
      0: '隐藏'
    };
    return cs[value];
  };
  const showType = (state) => {
    const stateMap = {
      1: 'success',
      0: 'info'
    };
    return stateMap[state];
  };

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      label: 'Id',
      width: 80,
      align: 'center'
    },
    {
      slot: 'display',
      label: '是否显示',
      width: 100,
      align: 'center'
    },
    {
      prop: 'seller',
      label: '所属商家',
      width: 120
    },
    {
      prop: 'title',
      label: '商品名称'
    },
    {
      prop: 'sales_price',
      label: '原价',
      width: 100
    },
    {
      prop: 'price',
      label: '优惠价',
      width: 100
    },
    {
      prop: 'share_bouns',
      label: '推荐奖励',
      width: 100
    },
    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 180
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 是否显示权限分配弹窗 */
  const showAuth = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageGoods({ ...where, ...orders, page, limit });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 打开权限分配弹窗 */
  const showImage = ref(false);
  const openImage = (row) => {
    current.value = row ?? null;
    showImage.value = true;
  };

  /** 显示状态 */
  const handleDisplay = (row) => {
    const display = row.display === 1 ? 0 : 1;
    updateGoodsDisplay({ id: row.id, display })
      .then(() => {
        row.display = display;
        EleMessage.success('操作成功');
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  };

  /** 删除单个 */
  const remove = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要删除“' + rows.map((d) => d.title).join(', ') + '”吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        removeGoods({ id })
          .then((msg) => {
            loading.close();
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>

<script>
  export default {
    name: 'SystemRole'
  };
</script>
